﻿namespace h1
{
    internal class StudentManager
    {
        private List<Student> students;

        public StudentManager()
        {
            students = new List<Student>();
        }

        // 1、增加一个学生
        public void AddStudent(Student student)
        {
            students.Add(student);
            Console.WriteLine("添加学生成功！");
        }

        // 2、修改一个指定学号的学生
        public void UpdateStudent(string studentId, string name, int age, string grade)
        {
            Student student = students.FirstOrDefault(s => s.StudentId == studentId);
            if (student != null)
            {
                student.Name = name;
                student.Age = age;
                student.Grade = grade;
                Console.WriteLine("修改学生信息成功！");
            }
            else
            {
                Console.WriteLine("未找到该学号的学生！");
            }
        }

        // 3、删除一个学生
        public void DeleteStudent(string studentId)
        {
            Student student = students.FirstOrDefault(s => s.StudentId == studentId);
            if (student != null)
            {
                students.Remove(student);
                Console.WriteLine("删除学生成功！");
            }
            else
            {
                Console.WriteLine("未找到该学号的学生！");
            }
        }

        // 4、查询一个指定学号的学生
        public void GetStudent(string studentId)
        {
            Student student = students.FirstOrDefault(s => s.StudentId == studentId);
            if (student != null)
            {
                Console.WriteLine(student);
            }
            else
            {
                Console.WriteLine("未找到该学号的学生！");
            }
        }

        // 5、遍历所有学生
        public void DisplayAllStudents()
        {
            if (students.Count == 0)
            {
                Console.WriteLine("没有学生信息！");
                return;
            }

            foreach (var student in students)
            {
                Console.WriteLine(student);
            }
        }
        static void Main(string[] args)
        {
            StudentManager manager = new StudentManager();

            // 测试增加学生
            manager.AddStudent(new Student("001", "张三", 20, "大一"));
            manager.AddStudent(new Student("002", "李四", 21, "大二"));
            manager.AddStudent(new Student("003", "王五", 22, "大三"));

            // 测试遍历
            Console.WriteLine("所有学生信息：");
            manager.DisplayAllStudents();

            // 测试修改
            manager.UpdateStudent("001", "张三丰", 20, "大一");
            Console.WriteLine("修改后：");
            manager.GetStudent("001");

            // 测试删除
            manager.DeleteStudent("002");
            Console.WriteLine("删除002后：");
            manager.DisplayAllStudents();

            // 测试查询
            Console.WriteLine("查询003：");
            manager.GetStudent("003");
        }
    }
}
